﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Models;
using BLL;
using MySql.Data.MySqlClient;


namespace LinSM
{
   /// <summary>
   /// 角色列表
   /// </summary>
    public partial class Characters : Form
    {
        public Characters()
        {
            InitializeComponent();
            dgvCharInfo.AutoGenerateColumns = false;//取消dgv自动增加列
        }

        CharManager cm = new CharManager();

        private void Selected()
        {
            if (dgvCharInfo.SelectedRows.Count != 0)
            {
                dgvCharInfo.CurrentRow.Selected = false;//不让这个控件有默认选中
            }
        }

        private void Characters_Load(object sender, EventArgs e)
        {
            dgvCharInfo.DataSource = cm.GetAllChar();
            Selected();
            tscboSearch.Text = "帐号";
        }

        private void dgvCharInfo_MouseClick(object sender, MouseEventArgs e)
        {
            if (have != null)
            {
                have.Close();
            }
            if (plModify.Visible == true)
            {
                plModify.Visible = false;
            }
        }

        public CharInfo have = null;
        private void dgvCharInfo_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            if (dgvCharInfo.SelectedRows.Count == 0)//没有选中数据行,返回
            {
                return;
            }
            if (have != null)
            {
                have.Close();
                return;
            }
            //如果没有打开过,就新建一个窗体
            CharInfo a = new CharInfo(this, dgvCharInfo);
            a.MdiParent = (Main)this.Tag;
            have = a;
            a.ShowDialog();
            //a.Show();
            
        }

        int AccType;
        private void tmsiModifyPassWord_Click(object sender, EventArgs e)
        {
            if (dgvCharInfo.SelectedRows.Count == 0)//没有选中数据行,返回
            {
                return;
            }
            lblTitle.Text = "修改密码";
            if (plModify.Visible == true)
            {
                plModify.Visible = false;
            }
            plModify.Visible = true;
            AccType = 1;
            txtAccount.Text = dgvCharInfo.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            plModify.Visible = false;
            txtAccount.Text = string.Empty;
            txtPwd1.Text = string.Empty;
            txtPwd2.Text = string.Empty;

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtAccount.Text == "")
            {
                MessageBox.Show("帐号不能为空");
                return;
            }
            if (txtPwd1.Text == "")
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            if (txtPwd1.Text != txtPwd2.Text)
            {
                MessageBox.Show("两次输入的密码不一致");
                return;
            }
            string acc = txtAccount.Text.Trim();
            string pwd = Convert.ToBase64String(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(txtPwd1.Text.Trim())));
           // if (cm.Account(acc, pwd, AccType)==1)
           // {
                MessageBox.Show(cm.Account(acc, pwd, AccType));
           // }
        }

        private void tsbtnCreateAccount_Click(object sender, EventArgs e)
        {
            lblTitle.Text = "添加新账号";
            if (plModify.Visible == true)
            {
                plModify.Visible = false;
            }
            plModify.Visible = true;
            txtAccount.ReadOnly = false;
            AccType = 2;
        }

        private void tsbtnUp_Click(object sender, EventArgs e)
        {
            cm.Page(0);//上一页
            dgvCharInfo.DataSource = cm.GetAllChar();
            Selected();
        }

        private void tsbtnDown_Click(object sender, EventArgs e)
        {
            if (cm.GetAllChar().Table.Rows.Count < cm.PageSize())//判断是否是最后一页
            {
                MessageBox.Show("已经是最后一页");
                return;
            }
            cm.Page(1);//下一页
            dgvCharInfo.DataSource = cm.GetAllChar();
            Selected();
        }

        private void tsbtnSearch_Click(object sender, EventArgs e)
        {
            string search = tstxtSearch.Text.Trim();
            if (search == "")
            {
                MessageBox.Show("请输入关键字！");
                tstxtSearch.Select();
                return;
            }
            string type = tscboSearch.SelectedItem.ToString();
            switch (type)
            {
                case "角色":
                    dgvCharInfo.DataSource = cm.Search(search, 1);
                    Selected();
                    break;
                default:
                    dgvCharInfo.DataSource = cm.Search(search, 0);
                    Selected();
                    break;
            }
        }

        private void tsmiGive_Click(object sender, EventArgs e)
        {
            if (dgvCharInfo.SelectedRows.Count == 0)//没有选中数据行,返回
            {
                return;
            }
            string name=dgvCharInfo.CurrentRow.Cells[1].Value.ToString();
            GiveChar gc = new GiveChar(name);
            gc.ShowDialog();
        }
    }
}
